package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.entity.SysGroupRelation;
import com.aiwei.tdjk.entity.SysGroupRelationExample;
import com.aiwei.tdjk.utils.ListUtil;
import com.aiwei.tdjk.utils.UserUtil;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import com.aiwei.tdjk.service.SysGroupRelationService;
import org.springframework.beans.factory.annotation.Autowired;

import com.aiwei.tdjk.dao.extend.SysGroupRelationExtendMapper;

import java.util.List;

@Service
public class SysGroupRelationServiceImpl implements SysGroupRelationService{

    @Autowired
    private SysGroupRelationExtendMapper sysGroupRelationExtendMapper;

    /**
     * 根据用户组查找用户id集合
     *
     * @param groupId
     */
    @Override
    public List<Long> findUserIdsByGroupId(Long groupId) {
        List<Long> userIds = Lists.newArrayList();
        SysGroupRelationExample sysGroupRelationExample = new SysGroupRelationExample();
        sysGroupRelationExample.or().andGroupIdEqualTo(groupId);
        List<SysGroupRelation> sysGroupRelationList = sysGroupRelationExtendMapper.selectByExample(sysGroupRelationExample);
        if(!ListUtil.isEmpty(sysGroupRelationList)){
            for (SysGroupRelation sysGroupRelation:sysGroupRelationList) {
                userIds.add(sysGroupRelation.getUserId());
            }
        }
        return userIds;
    }

    /**
     * 根据memberId获取组下的所有userIds
     *
     * @param memberId
     */
    @Override
    public List<Long> findUserIdsByMemberId(Long memberId) {
        List<Long> userIds = Lists.newArrayList();
        List<Long> groupIds = Lists.newArrayList();
        groupIds.add(29L);//管理员组
        SysGroupRelationExample sysGroupRelationExample = new SysGroupRelationExample();
        sysGroupRelationExample.or().andGroupIdIn(groupIds);

        List<SysGroupRelation> sysGroupRelationList = sysGroupRelationExtendMapper.selectByExample(sysGroupRelationExample);
        if(!ListUtil.isEmpty(sysGroupRelationList)){
            for (SysGroupRelation sysGroupRelation:sysGroupRelationList) {
                userIds.add(sysGroupRelation.getUserId());
            }
        }
        return userIds;
    }

    @Override
    public List<Long> findGroupIdsByLoginIngUser() {
        //获得当前登陆用户id
        Long userId = UserUtil.getUserFromSession().getId();
        List<Long> list = Lists.newArrayList();
        SysGroupRelationExample example = new SysGroupRelationExample();
        example.or().andUserIdEqualTo(userId);
        List<SysGroupRelation> sysGroupRelationList = sysGroupRelationExtendMapper.selectByExample(example);//通过userId查询所有
        if(sysGroupRelationList.size()>0){
            for(SysGroupRelation sysGroupRelation:sysGroupRelationList){
                list.add(sysGroupRelation.getGroupId());
            }
        }
        return list;
    }

    /**
     * 根据用户Id获取组Ids
     *
     * @param userId
     */
    @Override
    public List<Long> findGroupIdsByUserId(Long userId) {
        List<Long> list = Lists.newArrayList();
        SysGroupRelationExample example = new SysGroupRelationExample();
        example.or().andUserIdEqualTo(userId);
        List<SysGroupRelation> sysGroupRelationList = sysGroupRelationExtendMapper.selectByExample(example);//通过userId查询所有
        if(sysGroupRelationList.size()>0){
            for(SysGroupRelation sysGroupRelation:sysGroupRelationList){
                list.add(sysGroupRelation.getGroupId());
            }
        }
        return list;
    }
}
